var winObj = $(window);
var apiUrl = "http://"+window.location.host;
var alertTips = {
    uto: null,
    hide: function() {
        $('#poptips').hide();
    },
    show2: function(msg) {
        $('#poptips').html(msg).css({'top': (pageH - 100) * 0.5}).show();
    },
    show3: function(msg) {
        if (this.uto) {
            clearTimeout(this.uto);
        }
        $('#poptips').html(msg).css({'top': (pageH - 100) * 0.5}).show();
        this.uto = setTimeout(function() {
            alertTips.hide();
        }, 4000);
    },
    show: function(msg) {
        if (this.uto) {
            clearTimeout(this.uto);
        }
        $('#poptips').html(msg).css({'top': (pageH - 100) * 0.5}).show();
        this.uto = setTimeout(function() {
            alertTips.hide();
        }, 1500);
    }
}
var pageCtrl = {
    nowPage: 0,
    oldPage: 0,
    maxPage: 0,
    pageSc: 1,
    isAni: false,
    inited: false,
    data: {},
    resize: function(f) {
        if (pageCtrl.noResize) {
            return;
        }
        var pw = $('body').width();
        var ph = 1040;
        var tww = document.body.offsetWidth;
        var twh = document.body.offsetHeight;
        var wh = twh * pw / tww;
        var sc = tww / pw;
        //$('#main').height(wh + 2);
        var tw = Math.max(0, (pw - tww) * 0.5);
        if (sc <= 1) {
            tw = 0;
        }
        //$('#main').css({'transform': 'translate(' + tw + 'px, 0px) scale(' + sc + ')', '-webkit-transform': 'translate3d(' + tw + 'px, 0px, 0px) scale(' + sc + ')'});
        pageH = wh;
        this.pageSc = sc;

    },
    preInit: function() {
        $('.btns').each(function() {
            this.addEventListener('touchend', function(e) {
                e.preventDefault();
                var id = $(this).attr('id');
                switch (id) {
                    case "btn-login":
                        dologin();
                        break;
                }
            }, false, false);
        });
        window.addEventListener('touchstart', this, false);
        window.addEventListener('touchmove', this, false);
        window.addEventListener('touchend', this, false);
        window.addEventListener('orientationchange', this, false);
        $('#main').get(0).addEventListener("webkitAnimationEnd", this, false);
        this.resize();
        winObj.resize(function() {
            pageCtrl.resize();
        });
    }
}
function validPhone(num) {
    if (/^(13|14|15|17|18)\d{9}$/.test(num)) {
        return true;
    } else {
        return false;
    }
}
function dologin() {
    var name = $('input[name=name]').val();
    if (!name) {
        alertTips.show('请填写姓名');
        return;
    }
    var tel = $('input[name=tel]').val();
    if (!validPhone(tel)) {
        alertTips.show('请填写正确的手机号码');
        return;
    }
    $('input[name=name]').blur();
    $('input[name=name]').blur();
    $.ajax({
        type: 'POST',
        url: apiUrl + '/dologin',
        data: {'name': name, 'tel': tel},
        async: false,
        dataType: "json",
        headers: {
            'X-CSRF-TOKEN': $('input[name=_token]').val()
        },
        success: function(data) {
            if (data.success == true) {
                location.href = "/index";
            } else {
                alertTips.show(data.msg);
            }

        },
        error: function() {
            alertTips.show('亲，网络出现异常，请刷新再试');
        }
    });
}
function preload() {
    pageCtrl.preInit();
}
preload();